package cloud.pangui.core.utils;

import cloud.pangui.core.exception.UtilsRuntimeException;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Auther hepangui
 * @Date 2020/11/5 0005
 */
public class DateUtils {

	public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 获取当前时间，默认格式
	 *
	 * @return
	 */
	public static String now() {
		return now(DEFAULT_FORMAT);
	}

	/**
	 * 获取当前时间
	 *
	 * @param formatter
	 * @return
	 */
	public static String now(String formatter) {
		return toStr(new Date(), formatter);
	}

	/**
	 * 日期转字符串，默认格式
	 *
	 * @param date
	 * @return
	 */
	public static String toStr(Date date) {
		return toStr(date, DEFAULT_FORMAT);
	}

	/**
	 * 字符串转日期 默认格式
	 *
	 * @param dateStr
	 * @return
	 */
	public static Date toDate(String dateStr) {
		return toDate(dateStr, DEFAULT_FORMAT);
	}

	/**
	 * 日期转字符串
	 *
	 * @param date
	 * @param formattter
	 * @return
	 */
	public static String toStr(Date date, String formattter) {
		try {
			return new SimpleDateFormat(formattter).format(date);
		} catch (Exception e) {
			throw new UtilsRuntimeException(e);
		}
	}

	/**
	 * 字符串转date
	 *
	 * @param dateStr
	 * @param formatter
	 * @return
	 */
	public static Date toDate(String dateStr, String formatter) {
		try {
			return new SimpleDateFormat(formatter).parse(dateStr);
		} catch (Exception e) {
			throw new UtilsRuntimeException(e);
		}
	}

	/**
	 * 比较日期大小，日期格式为默认
	 */
	public static long compare(String strDate1, String strDate2) {
		return compare(toDate(strDate1), toDate(strDate2));
	}

	/**
	 * 比较日期大小
	 */
	public static long compare(Date date1, Date date2) {
		return date1.getTime() - date2.getTime();
	}


	/**
	 * 增加天数
	 *
	 * @return
	 */
	public static Date addDay(Date date, int days) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, days);
		return c.getTime();
	}

	/**
	 * 增加月份
	 */
	public static Date addMonth(Date date, int months) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, months);
		return c.getTime();
	}

	/**
	 * 增加小时
	 */
	public static Date addHour(Date date, int hours) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.HOUR, hours);
		return c.getTime();
	}

	/**
	 * 增加分钟
	 */
	public static Date addMinutes(Date date, int minutes) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, minutes);
		return c.getTime();
	}

	/**
	 * 增加毫秒
	 */
	public static Date addMillisecond(Date date, int millisecond) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MILLISECOND, millisecond);
		return c.getTime();
	}

	/**
	 * 获取某日期的年份
	 */
	public static int getYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.YEAR);
	}

	/**
	 * 获取某日期的月份
	 *
	 * @param date
	 * @return
	 */
	public static Integer getMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取某日期的日数
	 */
	public static Integer getDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int day = cal.get(Calendar.DATE);//获取日
		return day;
	}

	/**
	 * 获取指定日期所在周的第一天
	 */
	public static Date getFirstDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
		return c.getTime();
	}

	/**
	 * 获取指定日期所在周的最后一天
	 */
	public static Date getLastDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
		return c.getTime();
	}

	/**
	 * 获取某年某周的第一天
	 */
	public static Date getFirstDayOfWeek(int year, int week) {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, Calendar.JANUARY);
		c.set(Calendar.DATE, 1);

		Calendar cal = (GregorianCalendar) c.clone();
		cal.add(Calendar.DATE, week * 7);

		return getFirstDayOfWeek(cal.getTime());
	}

	/**
	 * 获取某年某周的最后一天
	 *
	 * @return
	 */
	public static Date getLastDayOfWeek(int year, int week) {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, Calendar.JANUARY);
		c.set(Calendar.DATE, 1);

		Calendar cal = (GregorianCalendar) c.clone();
		cal.add(Calendar.DATE, week * 7);

		return getLastDayOfWeek(cal.getTime());
	}

	/**
	 * 获取某年某月的第一天
	 *
	 * @return
	 */
	public static Date getFirstDayOfMonth(int year, int month) {
		month = month - 1;
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month);

		int day = c.getActualMinimum(c.DAY_OF_MONTH);

		c.set(Calendar.DAY_OF_MONTH, day);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}

	/**
	 * 获取某年某月的最后一天
	 */
	public static Date getLastDayOfMonth(int year, int month) {
		month = month - 1;
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month);
		int day = c.getActualMaximum(c.DAY_OF_MONTH);
		c.set(Calendar.DAY_OF_MONTH, day);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		return c.getTime();
	}

	/**
	 * 获取这是当月的第几周
	 */
	public static int getWeekOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		//第几周
		int week = calendar.get(Calendar.WEEK_OF_MONTH);
		//第几天，从周日开始
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		if (day == 1) {
			week -= 1;
		}

		date = getFirstDayOfWeek(date);
		day = calendar.get(Calendar.DAY_OF_WEEK);
		if (day == 1) {
			week += 1;
		}
		return week;
	}

}
